package com.yy.rocketmq.rocketmq;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("mymqproducer")
@Slf4j
public class MyMQProducer {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    public void send(String topic, String msg) {
        rocketMQTemplate.convertAndSend(topic, msg);
    }

    //同步发送
    public void syncSend(String topic, String msg) {
        rocketMQTemplate.syncSend(topic, msg);
    }

    //异步发送
    public void asyncSend(String topic, String msg) {
        rocketMQTemplate.asyncSend(topic, msg, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                log.info("async send result:{}", sendResult);
            }

            @Override
            public void onException(Throwable throwable) {
                log.info("asyncSend error", throwable);
            }
        });
    }
}
